<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends CI_Controller {
     function __construct(){
        parent::__construct();
        $this->load->model('picture_m');
        }
        
/* 
 * Upload del logo
 */
    public function upload_logo(){
        $this->load->library('generales');
        $id_config = $this->input->post('id'); //id del registro pertenece a la configuracion.
        
        $img_name = $this->generales->generate_string(5);
        
        //Configuracion del upload
        $config['upload_path'] = './web/img/logo/';
        $config['allowed_types'] = 'gif|jpg|png';
	$config['max_size'] = '2048';
        $config['remove_spaces'] = TRUE;
        $config['overwrite'] = TRUE;
        $config['file_name'] = $img_name;
        
	$this->load->library('upload', $config);
        
        if (!$this->upload->do_upload()){
             $this->session->set_flashdata('error', $this->upload->display_errors());
             redirect('admin/config_gral');
	}else{
            $dataok = array('upload_data' => $this->upload->data());
                foreach($dataok as $clave => $valor){ 
                     //Se crea el thumbnail
                     $config['image_library'] = 'GD2';
                     $config['source_image'] = $valor['full_path'];
                     $config['create_thumb'] = FALSE;
                     $config['master_dim'] = 'auto';
                     $config['maintain_ratio'] = TRUE;
                     $config['height'] = 200;
                     $config['width'] = 200;

                     $this->load->library('image_lib', $config);
                     
                      if (!$this->image_lib->resize()){
                        $this->session->set_flashdata('error', $this->upload->display_errors());
                        redirect('admin/config_gral');
                        }
                        $this->load->model('config_m');
                        $this->config_m->update_logo($id_config,$valor['file_name']);
                        
                        $this->session->set_flashdata('mensaje', 'Logo cargado con éxito');
                        redirect('admin/config_gral');
                    }
		}
        }
        
     /*
      * Subida de imagenes para la carga de inmueble.
      */   
     public function uploadifive(){
          //recupero el id del formulario desde la sesion  
          $id_form = $this->session->userdata('id_formulario');
          $id_inmu = $this->session->userdata('id_new_inmu');
          if (!empty($_FILES)){
              $this->load->library('generales');
              $randomname = $this->generales->generate_string(5);
              $name_img = strtolower(str_replace(" ", "",$randomname.$_FILES['Filedata']['name']));
              $tempFile = $_FILES['Filedata']['tmp_name'];
              $path_img = './web/img/inmu/';
              $targetFile = (str_replace('//','/',$path_img).$name_img);
                
              if (!@copy($tempFile, $targetFile)){
                  if (!@move_uploaded_file($tempFile, $targetFile)){
                      echo "Error";
                 }else
                     echo str_replace($_SERVER['DOCUMENT_ROOT'], '', $targetFile);
                    }
                    else
                        echo str_replace($_SERVER['DOCUMENT_ROOT'], '', $targetFile);
                   
                    //Guardo en la base los datos de la imagen.
                    $this->picture_m->save_img($name_img,$path_img,$id_form, $id_inmu);
                    //guardo la variable de sesion con el id del formulario.
                    //$this->session->set_userdata('id_formulario', $id_form);
                }
             
      }
            
            
      /*
       * Crear Thumbnails     
       */
        public function crear_thumb($id_form){
            $imagen_data = $this->picture_m->search_img($id_form);
            if(count($imagen_data)>0){
                $this->load->library('image_lib');
                foreach ($imagen_data->result() as $fila){
                   $name_pic = $fila->name_img;
                   $source_image = $fila->path_img;
                  //configuracion para crear thumbs
                    $config['image_library'] = 'GD2';
                    $config['source_image'] = $source_image.$name_pic;
                    $config['new_image'] = './web/img/mini/';
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 120;
                    $config['height'] = 120;
                    $config['master_dim'] = 'auto';
                    $config['quality'] = 90;
                    $this->image_lib->initialize($config);
                    if(!$this->image_lib->resize()){
                        echo $this->image_lib->display_errors();
                        }else{
                            $this->image_lib->clear();
                            }     
                   //config para crear el small         
                    $config['image_library'] = 'GD2';
                    $config['source_image'] = $source_image.$name_pic;
                    $config['new_image'] = './web/img/small/';
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 400;
                    $config['height'] = 400;
                    $config['master_dim'] = 'auto';
                    $config['quality'] = 90;
                    $this->image_lib->initialize($config);
                    if(!$this->image_lib->resize()){
                        echo $this->image_lib->display_errors();
                        }else{
                            $this->image_lib->clear();
                            }
                   }  
              $this->view_thumbs($id_form);
            }  
      }
      
      /*
       * Muestra los thumbs de las imagenes subidas
       */
      public function view_thumbs($id_form){
          $imagen_data['imagen_data'] = $this->picture_m->search_img($id_form);
          $this->load->view('upload/v_thumb',$imagen_data);
      }


  
}//Fin de clase Upload

/***
 * Valores del array data que devuelve al subir la imagen
file_name: foto.jpg
file_type: image/jpeg
file_path: D:/xampp/htdocs/xtudent/images/uploads/logos/
full_path: D:/xampp/htdocs/xtudent/images/uploads/logos/foto.jpg
raw_name: foto
orig_name: foto.jpg
client_name: foto.jpg
file_ext: .jpg
file_size: 16.11
is_image: 1
image_width: 320
image_height: 320
image_type: jpeg
image_size_str: width="320" height="320"
 * 
 * 
 */
